import 'package:flutter/material.dart';
import 'package:hrcapp/utils/theme_colors.dart';

class HRCAssetAnchoringScreen extends StatelessWidget {
  const HRCAssetAnchoringScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.background,
      appBar: AppBar(
        backgroundColor: ThemeColors.cardBg,
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, size: 16, color: Colors.black),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: const Text(
          '资产价值锚定',
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children:
              [
                // 流程指示器
                _buildProcessIndicator(),
                const SizedBox(height: 40),

                // 资产信息卡片
                _buildAssetInfoCard(),
                const SizedBox(height: 40),

                // 价值锚定信息
                _buildAnchoringInfo(),
                const SizedBox(height: 40),

                // 底部操作按钮
                _buildConfirmButton(context),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 构建流程指示器
  Widget _buildProcessIndicator() {
    return SizedBox(
      height: 80,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children:
        [
          _buildStepIndicator('资产上传', false, '1'),
          _buildStepConnector(false),
          _buildStepIndicator('认证评估', false, '2'),
          _buildStepConnector(true),
          _buildStepIndicator('价值锚定', true, '3'),
          _buildStepConnector(false),
          _buildStepIndicator('生成通证', false, '4'),
          _buildStepConnector(false),
          _buildStepIndicator('上链完成', false, '5'),
        ],
      ),
    );
  }

  // 构建步骤指示器
  Widget _buildStepIndicator(String label, bool isActive, String number) {
    return Column(
      children:
      [
        CircleAvatar(
          radius: 15,
          backgroundColor: isActive ? ThemeColors.secondary : const Color(0xFFE5E6EB),
          child: Text(
            number,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
        ),
        const SizedBox(height: 10),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: isActive ? ThemeColors.secondary : const Color(0xFF86909C),
          ),
        ),
      ],
    );
  }

  // 构建步骤连接器
  Widget _buildStepConnector(bool isActive, [bool isDashed = false]) {
    return Expanded(
      child:
      Container(
        height: 2,
        margin: const EdgeInsets.symmetric(horizontal: 4),
        color: isActive ? ThemeColors.secondary : const Color(0xFFE5E6EB),
      ),
    );
  }

  // 构建资产信息卡片
  Widget _buildAssetInfoCard() {
    return Container(
      width: double.infinity,
      height: 120,
      decoration: BoxDecoration(
        color: ThemeColors.cardBg,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child:
        Row(
          children:
          [
            // 资产图片
            Container(
              width: 80,
              height: 80,
              decoration: BoxDecoration(
                color: const Color(0xFFF5F5F5),
                borderRadius: BorderRadius.circular(8),
              ),
              child: CustomPaint(
                painter: ScrollPatternPainter(),
              ),
            ),
            const SizedBox(width: 20),
            // 资产信息
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children:
              [
                const Text(
                  '清代苏绣花鸟图轴',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  '评估编号：HR20230515001',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  '评估日期：2023-05-20',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 构建价值锚定信息
  Widget _buildAnchoringInfo() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(30),
      decoration: BoxDecoration(
        color: ThemeColors.cardBg,
        borderRadius: BorderRadius.circular(12),
      ),
      child:
      Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children:
        [
          const Text(
            '锚定依据',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colors.black,
            ),
          ),
          const SizedBox(height: 40),

          // 评估得分
          _buildScoreBar('艺术价值得分', 88, 220),
          const SizedBox(height: 30),
          _buildScoreBar('历史价值得分', 92, 250),
          const SizedBox(height: 30),
          _buildScoreBar('保存状况得分', 73, 200),
          const SizedBox(height: 40),

          // 综合价值
          Container(
            width: double.infinity,
            height: 60,
            decoration: BoxDecoration(
              color: const Color(0xFFF0F2F5),
              borderRadius: BorderRadius.circular(6),
            ),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child:
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children:
                [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children:
                    [
                      const Text(
                        '综合评估价值',
                        style: TextStyle(
                          fontSize: 14,
                          color: Colors.black,
                        ),
                      ),
                      Text(
                        '= 艺术价值(40%) + 历史价值(30%) + 保存状况(30%)',
                        style: TextStyle(
                          fontSize: 12,
                          color: ThemeColors.textSecondary,
                        ),
                      ),
                    ],
                  ),
                  const Text(
                    '85.7/100',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: Colors.black,
                    ),
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(height: 20),

          // 锚定结果
          Container(
            width: double.infinity,
            height: 60,
            decoration: BoxDecoration(
              color: const Color(0xFFE6F7FF),
              borderRadius: BorderRadius.circular(6),
            ),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child:
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children:
                [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children:
                    [
                      const Text(
                        '锚定HKDG金额',
                        style: TextStyle(
                          fontSize: 14,
                          color: Colors.black,
                        ),
                      ),
                      Text(
                        '1 HKDG = 1 港币 (离岸人民币汇率: 1:0.92)',
                        style: TextStyle(
                          fontSize: 12,
                          color: ThemeColors.textSecondary,
                        ),
                      ),
                    ],
                  ),
                  const Text(
                    '¥286,500',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: ThemeColors.secondary,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 构建得分条
  Widget _buildScoreBar(String label, int score, double fillWidth) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children:
      [
        Text(
          label,
          style: TextStyle(
            fontSize: 14,
            color: ThemeColors.textSecondary,
          ),
        ),
        const SizedBox(height: 10),
        Stack(
          children:
          [
            Container(
              width: double.infinity,
              height: 8,
              decoration: BoxDecoration(
                color: const Color(0xFFE5E6EB),
                borderRadius: BorderRadius.circular(4),
              ),
            ),
            Container(
              width: fillWidth,
              height: 8,
              decoration: BoxDecoration(
                color: ThemeColors.success,
                borderRadius: BorderRadius.circular(4),
              ),
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children:
          [
            Text(
              '$score/100',
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
                color: ThemeColors.success,
              ),
            ),
          ],
        ),
      ],
    );
  }

  // 构建确认按钮
  Widget _buildConfirmButton(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 50,
      decoration: BoxDecoration(
        color: ThemeColors.secondary,
        borderRadius: BorderRadius.circular(25),
      ),
      child:
      TextButton(
        onPressed: () {
          // 确认锚定的逻辑
          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(content: Text('资产价值锚定成功')),
          );
          // 跳转到生成通证页面
          Navigator.pushNamed(context, '/generateToken');
        },
        child:
        const Text(
          '确认锚定',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}

// 绘制卷轴图案的Painter
class ScrollPatternPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = const Color(0xFFD4B16A)
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    // 绘制三个斜线条
    canvas.drawLine(Offset(size.width * 0.2, size.height * 0.4),
        Offset(size.width * 0.4, size.height * 0.6), paint);
    canvas.drawLine(Offset(size.width * 0.3, size.height * 0.3),
        Offset(size.width * 0.5, size.height * 0.5), paint);
    canvas.drawLine(Offset(size.width * 0.4, size.height * 0.4),
        Offset(size.width * 0.6, size.height * 0.6), paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}